package com.example.toutiao.ui.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.toutiao.R;
import com.example.toutiao.adapter.ArticleAdapter;
import com.example.toutiao.db.Article;
import com.example.toutiao.db.DatabaseHelper;

import java.util.List;

//文章列表
public class ArticleListActivity extends AppCompatActivity {

    private String username;
    private RecyclerView recyclerView;
    private List<Article> articleList;
    private ArticleAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article_list);
        TextView tabBarTitle = findViewById(R.id.tabBarTitle);
        tabBarTitle.setText("文章列表");
        if (getIntent() != null) {
            username = getIntent().getStringExtra("username");
        }
        SharedPreferences sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE);
        String currentUsername = sharedPreferences.getString("username", null);
        DatabaseHelper dbHelper = new DatabaseHelper(this);
        List<Article> articles = dbHelper.getArticlesByUsername(username);


        recyclerView = findViewById(R.id.recycler_articles);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        dbHelper = new DatabaseHelper(this);
        articleList = dbHelper.getArticlesByUsername(currentUsername);

        adapter = new ArticleAdapter(this, articleList);
        recyclerView.setAdapter(adapter);

        if (articles.isEmpty()) {
            Toast.makeText(this, "没有找到您的文章", Toast.LENGTH_SHORT).show();
        }
    }
}
